import { getGameObjectById, core, GameObject, TextRenderer, HitTestScript } from "../gameengine";
import { conversationPrefab } from "../data/data";
import { ConversationMachine } from "./conversationMachine";
import {lastTouch, lastTouchGameObject, MainRoleBehaviour} from "../mainRoleBehaviour";

export type sentence = {
    name: string,
    content: string
}

export class Conversation {
    public displayObject:GameObject;
    public sentences: sentence[];
    public currentSentence: number = 0;
    public conversationMachine: ConversationMachine;

    private prefab: GameObject;

    private dialogTextrenderder: TextRenderer;
    private nameTextRenderer: TextRenderer;

    private mainRoleBehaviour: MainRoleBehaviour;


    constructor() {
        this.conversationMachine = new ConversationMachine();
        this.conversationMachine.conversation = this;
        this.mainRoleBehaviour = getGameObjectById("mainRole").getScript(MainRoleBehaviour);
    }

    dialogBoxInit() {
        const container = getGameObjectById("Container");
        if(getGameObjectById("conversationPrefab")){
            container.removeChild(getGameObjectById("conversationPrefab"));
        }

        console.log("box init!");


        const conversationGameobject = core.createPrefab(conversationPrefab);
        container.addChild(conversationGameobject);

        const dialogTxt = getGameObjectById('dialogTxt');
        const nameTxt = getGameObjectById('nameTxt');
        this.prefab = getGameObjectById("conversationPrefab");

        this.dialogTextrenderder = dialogTxt.getScript(TextRenderer);
        this.nameTextRenderer = nameTxt.getScript(TextRenderer);

        console.log(container.children);

    }

    public nextSentence() {

        const lastInteractionObject=this.mainRoleBehaviour.getLastInteractionObject();

        // if(this.displayObject!=lastTouch.obj){
        //     return;
        // }

        console.log("this.currentSentence");
        console.log(this.currentSentence);

        console.log("lastTouch.obj");
        console.log(lastTouch.obj);
        console.log("this.displayObject");
        console.log(this.displayObject);


        if (this.currentSentence >= this.sentences.length) {
            this.stopConversation();
        } else {
            this.dialogTextrenderder.text = this.sentences[this.currentSentence].content;
            this.nameTextRenderer.text = this.sentences[this.currentSentence].name;

            this.currentSentence++;
        }
    }

    onKeyDown = ev => {
        switch (ev.keyCode) {
            case 32:    //space是32
                console.log("keyDowm next sentence")
                console.log(this.displayObject);
                this.nextSentence();
                // console.log("nextSentence");
                break;
        }
    };

    public addListener() {
        // window.onkeydown=null;
        console.log(window.onkeydown);

        this.mainRoleBehaviour.removeOnKeyDownListener();
        window.addEventListener("keydown", this.onKeyDown);

        console.log("add listener")
    }


    public stopConversation() {
        window.removeEventListener("keydown", this.onKeyDown);
        // window.removeEventListener("keyup", this.onKeyDown);
        this.mainRoleBehaviour.addOnKeyDownListener();
        const container = getGameObjectById("Container");
        const prefab=getGameObjectById("conversationPrefab");
        // container.removeChild(this.prefab);
        container.removeChild(prefab);
        // const itemColliders = getGameObjectById("Scene").getScript(ColliderSystem).itemColliders;
        // const item = itemColliders[0];
        // if (!item) {
        //     return;
        // }

        // console.log(conversationTest);
        // if (item.canTalk == false) {
        //     return;
        // }
        // const conversationTest = getGameObjectById(item.id).getScript(ConversationTest);
        // conversationTest.ChangeState('none');
        this.currentSentence = 0;
        console.log("结束对话，切换对话框状态");
        console.log(container.children);
    }

    public beginConversation() {
        this.dialogBoxInit();
        this.addListener();
        this.nextSentence();
    }
}